package com.openess.bigsearch.engine.BTree.util;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;

public class Memory {
	public static void main(String args[]) {
		System.out.println("usage:");
		long max = showMax();
		System.out.println("Max:" + max + "MBytes");
		long t = showUsage();
		System.out.println("Total:" + t + "MBytes");
		long fr = freeMemory();
		// System.gc();  
		System.out.println("Free:" + fr + "MBytes");
		long rem = t - fr;
		System.out.println("OccupiedSpace:" + rem + "MBytes");
		long hf = heapFree();
		System.out.println("HeapFree:" + hf + "MBytes");
	}

	public static long showUsage() {
		long l = Runtime.getRuntime().totalMemory();
		return l / 1024 / 1024;
	}

	public static long showMax() {
		long m = Runtime.getRuntime().maxMemory();
		return m / 1024 / 1024;
	}

	public static long freeMemory() {
		long f = Runtime.getRuntime().freeMemory();
		return f / 1024 / 1024;
	}

	public static long heapFree() {
		MemoryMXBean mm = (MemoryMXBean) ManagementFactory.getMemoryMXBean();
		long used = mm.getHeapMemoryUsage().getUsed() / 1024 / 1024;
		long total = mm.getHeapMemoryUsage().getMax() / 1024 / 1024;
		return total - used;
	}
}
